package com.yztc.orangeentertainment.ui.main.minefragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.bean.UserBean;
import com.yztc.orangeentertainment.ui.login.LoginActivity;
import com.yztc.orangeentertainment.ui.main.ArticleShouCang.ArticleShouCangActivity;
import com.yztc.orangeentertainment.utils.CircleImageView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/5.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {
    @InjectView(R.id.login_head)
    CircleImageView mLogin;
    @InjectView(R.id.rel_mine_task)
    RelativeLayout mTask;
    @InjectView(R.id.rel_mine_mall)
    RelativeLayout mMall;
    @InjectView(R.id.rel_mine_skin)
    RelativeLayout mSkin;
    @InjectView(R.id.rel_mine_setting)
    RelativeLayout mSetting;
    @InjectView(R.id.rel_mine_msg)
    RelativeLayout mMsg;
    @InjectView(R.id.favorite)
    RelativeLayout mFavorite;
    @InjectView(R.id.switch_mine)
    Switch mMine;
    @InjectView(R.id.rel_mine_invite)
    RelativeLayout mInvite;
    @InjectView(R.id.rel_mine_help)
    RelativeLayout mHelp;
    public static final int REQUEST_CODE = 1;
    @InjectView(R.id.login_back)
    CircleImageView loginBack;
    @InjectView(R.id.tv_loginscore)
    TextView tvLoginscore;
    @InjectView(R.id.tv_username)
    TextView tvUsername;
    @InjectView(R.id.mine_zhongxin)
    Button mineZhongxin;
    @InjectView(R.id.mine_mall)
    Button mineMall;
    private String id;
    private Bitmap bm;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initShow();

        return rootView;
    }

    private void initData() {

        Log.i("qwe",id);
        final UserBean userBean = App.getLiteOrm().queryById(id, UserBean.class);
        if(userBean!=null) {
            tvUsername.setText(userBean.getUserName());
            tvLoginscore.setText("等级: 1");
            tvLoginscore.setTextColor(Color.WHITE);
            if (userBean.getUserPicture()!=null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            HttpURLConnection conn = (HttpURLConnection) new URL(userBean.getUserPicture()).openConnection();
                            InputStream in = conn.getInputStream();
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int len;

                            while ((len = in.read(buffer)) != -1) {
                                baos.write(buffer, 0, len);
                            }
                            byte[] result = baos.toByteArray();
                            bm = BitmapFactory.decodeByteArray(result, 0, result.length);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                mLogin.setImageBitmap(bm);
            }else {
                mLogin.setImageResource(R.mipmap.ic_portrait_logout);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    private void initShow() {
        mLogin.setOnClickListener(this);
        mFavorite.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_head:
                startActivityForResult(new Intent(getActivity(), LoginActivity.class), REQUEST_CODE);
                break;
            case R.id.favorite:
                startActivity(new Intent(getActivity(), ArticleShouCangActivity.class));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        id=getArguments().getString("id");
        if (id!=null)
        initData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //通过请求码和结果码判断为本次请求
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            id = data.getStringExtra("id");
        }
    }
}
